<?php
declare(strict_types=1);

namespace App\ZxAdmin\Content;

use App\Controller;
use Field\SysArticleConst;
use Field\SysArticleColumns;
use Field\SysArticleRow;

// hook controller_zx_admin_content_articles_use.php

/**
 * @title   文章系统
 * @auth    true
 * @login   true
 * @menu    true
 * @component   vlist/index.vue
 * @icon    el-icon-collection
 * @active  true
 * @affix   false
 * @rank    99
 */
class Articles extends Controller
{
    // hook controller_zx_admin_content_articles_public_start.php



    // hook controller_zx_admin_content_articles_public_end.php

    /**
     * @title  下发配置
     * @rank    0
     */
    public function action_Conf_PUT()
    {
        // hook controller_zx_admin_content_articles_conf_start.php
        $conf=['pageSize'=>'50','remoteSort'=>0,'remoteFilter'=>0,'remoteSummary'=>0,'hidePagination'=>0,'hideRefresh'=>0,'hideDo'=>0,'add'=>1,'start'=>0,'stop'=>0,'delete'=>1,'import'=>0,'export'=>0,'pageSizes'=>[10,20,25,50,100,500,1000,10000],'enable'=>1,'disable'=>1];
        $Columns = [['table'=>'SysArticle','prop'=>'ArticleID','label'=>'文章ID','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'CateID','label'=>'分类ID','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'UserID','label'=>'用户','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'AttachID','label'=>'缩略图ID','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'editable'=>1,'type'=>'image','disabled'=>1],['table'=>'SysArticle','prop'=>'Title','label'=>'文章标题','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'editable'=>1,'type'=>'input'],['table'=>'SysArticle','prop'=>'Points','label'=>'积分','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'editable'=>1,'type'=>'number'],['table'=>'SysArticle','prop'=>'Rmbs','label'=>'余额','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'editable'=>1,'type'=>'number'],['table'=>'SysArticle','prop'=>'IsTop','label'=>'置顶','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>1,'editable'=>1,'type'=>'dict','dict'=>'On'],['table'=>'SysArticle','prop'=>'IsRecom','label'=>'推荐','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>1,'editable'=>1,'type'=>'dict','dict'=>'On'],['table'=>'SysArticle','prop'=>'IsFree','label'=>'免费','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>1,'editable'=>1,'type'=>'dict','dict'=>'On'],['table'=>'SysArticle','prop'=>'IsVideo','label'=>'视频','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'batch'=>0,'editable'=>0],['table'=>'SysArticle','prop'=>'IsImage','label'=>'图片','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'batch'=>0,'editable'=>0],['table'=>'SysArticle','prop'=>'CreateAt','label'=>'录入','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'UpdateAt','label'=>'更新','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'Status','label'=>'状态','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'Rank','label'=>'排序','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'Views','label'=>'访问量','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'Likes','label'=>'点赞量','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysArticle','prop'=>'ReplyNum','label'=>'回帖量','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0]];
        $Filter = [['table'=>'SysArticle','prop'=>'CateID','label'=>'分类ID','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'select','multiple'=>0,'mode'=>'=','span'=>4],['table'=>'SysArticle','prop'=>'UserID','label'=>'用户','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'=','component'=>'select','multiple'=>0,'span'=>4],['table'=>'SysArticle','prop'=>'Title','label'=>'文章标题','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'LI','component'=>'input','span'=>4],['table'=>'SysArticle','prop'=>'IsTop','label'=>'置顶','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'=','component'=>'radioGroup','dict'=>'On','span'=>4],['table'=>'SysArticle','prop'=>'IsRecom','label'=>'推荐','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'radioGroup','mode'=>'=','dict'=>'On','span'=>4],['table'=>'SysArticle','prop'=>'IsFree','label'=>'免费','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'radioGroup','dict'=>'On','mode'=>'=','span'=>4],['table'=>'SysArticle','prop'=>'IsVideo','label'=>'视频','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'radioGroup','mode'=>'=','dict'=>'On','span'=>4],['table'=>'SysArticle','prop'=>'IsImage','label'=>'图片','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'radioGroup','mode'=>'=','dict'=>'On','span'=>4],['table'=>'SysArticle','prop'=>'CreateAt','label'=>'录入','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'date','mode'=>'><','type'=>'daterange','valueFormat'=>'YYYY-MM-DD','span'=>4],['table'=>'SysArticle','prop'=>'Status','label'=>'状态','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'=','component'=>'radioGroup','dict'=>'On','span'=>4]];
        $Tools = [];

        // hook controller_zx_admin_content_articles_conf_end.php
        $this->response('0000', ['Conf' =>$conf, 'Filter' => $Filter, 'Tools' =>$Tools, 'Columns' => $Columns]);
    }

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin_content_articles_index_start.php
        $page = $this->request->param('page', 0);
        $size = $this->request->param('limit', 0);
        $field = $this->request->param('field', 'ArticleID');
        $sort = $this->request->param('sort', -1);
        $order = [];
        !empty($field) && $order[$field] = $sort == 'asc' ? -1 : 1;
        $cond = [];
        $CateID=$this->request->param('CateID',-1);
		!empty($CateID) && $cond['CateID']=$CateID;
		$UserID=$this->request->param('UserID',-1);
		!empty($UserID) && $cond['UserID']=$UserID;
		$Title=$this->request->param('Title');
		!empty($Title) && $cond['Title']['LI']=$Title;
		$IsTop=$this->request->param('IsTop',-1);
		$IsTop!=-1 && $cond['IsTop']=$IsTop;
		$IsRecom=$this->request->param('IsRecom',-1);
		$IsRecom!=-1 && $cond['IsRecom']=$IsRecom;
		$IsFree=$this->request->param('IsFree',-1);
		$IsFree!=-1 && $cond['IsFree']=$IsFree;
		$IsVideo=$this->request->param('IsVideo',-1);
		$IsVideo!=-1 && $cond['IsVideo']=$IsVideo;
		$IsImage=$this->request->param('IsImage',-1);
		$IsImage!=-1 && $cond['IsImage']=$IsImage;
		$CreateAt=$this->request->param('CreateAt',[]);
		!empty($CreateAt) && $cond['CreateAt']['><']=[$CreateAt[0].' 00:00:00',$CreateAt[1].' 23:59:59'];
		$Status=$this->request->param('Status',-1);
		$Status!=-1 && $cond['Status']=$Status;
        $data = $this->SysArticle->GetList($cond, $order, $page, $size);
        
        foreach ($data['data'] as &$row) {
            
            //unset($row['PassWord'],$row['Salt']);
        }

        // hook controller_zx_admin_content_articles_index_end.php
        $this->response('0000', $data);
    }
    
    /**
     * @title   新增配置
     * @rank    72
     */
    public function action_Add_PUT()
    {
        // hook controller_zx_admin_content_articles_add_put_start.php
        $AddForm = [['table'=>'SysArticle','prop'=>'CateID','label'=>'分类ID','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'span'=>6,'validator'=>'^[0-9]*$','component'=>'select','multiple'=>0],['table'=>'SysArticle','prop'=>'Title','label'=>'文章标题','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'span'=>6,'validator'=>'^[\\u4E00-\\u9FA5A-Za-z0-9]+$','component'=>'input','multiple'=>0],['table'=>'SysArticle','prop'=>'Points','label'=>'积分','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'span'=>6,'validator'=>'^[0-9]*$','component'=>'input'],['table'=>'SysArticle','prop'=>'Rmbs','label'=>'余额','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'span'=>6,'validator'=>'^[0-9]*$','component'=>'input'],['table'=>'SysArticle','prop'=>'IsRecom','label'=>'推荐','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'span'=>6,'validator'=>'^[0-9]*$','component'=>'radioGroup','dict'=>'On'],['table'=>'SysArticle','prop'=>'IsFree','label'=>'免费','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'span'=>6,'validator'=>'^[0-9]*$','component'=>'radioGroup','dict'=>'On']];
        // hook controller_zx_admin_content_articles_add_put_end.php
        $this->response('0000', ['data' => $AddForm]);
    }

    /**
     * @title   新增提交
     * @rank    71
     */
    public function action_Add_POST()
    {
        // hook controller_zx_admin_content_articles_add_post_start.php
        $data = [];
        $data['CateID']=$this->request->param('CateID',0);
		$data['Title']=$this->request->param('Title',);
		$data['Points']=$this->request->param('Points',0);
		$data['Rmbs']=$this->request->param('Rmbs',0);
		$data['IsRecom']=$this->request->param('IsRecom',0);
		$data['IsFree']=$this->request->param('IsFree',0);
        $r = $this->SysArticle->insert($data);
        // make addformpost SysArticle ZxAdmin\Content
        // hook controller_zx_admin_content_articles_add_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    
    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zx_admin_content_articles_field_post_start.php
        $cond = [];
        $cond['ArticleID']=$this->request->param('ArticleID',0);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field,...$cond], ['待修改数据','待修改字段']);
        $field_fields = ['AttachID','Title','Points','Rmbs','IsTop','IsRecom','IsFree'];
        $tmp_field=rtrim($field,'+-*/=');
        !in_array($tmp_field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($tmp_field, SysArticleConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($tmp_field) {
        case 'AttachID':
         case 'Points':
         case 'Rmbs':
         case 'IsTop':
         case 'IsRecom':
         case 'IsFree':
             $update[$field] = intval($value);
            break;
         case 'Title':
             $update[$field] = $value;
            break;
        }
        $r = !empty($update) ? $this->SysArticle->update($cond, $update) : false;
        // hook controller_zx_admin_content_articles_field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    /**
     * @title   批量编辑字段
     * @rank    60
     */
    public function action_Update_POST()
    {
        // hook controller_zx_admin_content_articles_update_post_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
            isset($row['ArticleID']) && $cond['ArticleID'][]=intval($row['ArticleID']);
        }
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field,...$cond], ['待修改数据','字段']);
        $update_fields = ['IsTop','IsRecom','IsFree'];
        $tmp_field=rtrim($field,'+-*/=');
        !in_array($tmp_field, $update_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($tmp_field, SysArticleConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($tmp_field) {
        case 'IsTop':
         case 'IsRecom':
         case 'IsFree':
             $update[$field] = intval($value);
            break;
        }
        $r = !empty($update) ? $this->SysArticle->update($cond, $update) : false;
        // hook controller_zx_admin_content_articles_update_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    

    
    /**
     * @title   启用
     * @rank    41
     */
    public function action_Enable_POST()
    {
        // hook controller_zx_admin_content_articles_enable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                isset($row['ArticleID']) && $cond['ArticleID'][]=intval($row['ArticleID']);
        }
        $this->CheckEmpty($cond, ['待修改数据']);
        $r = $this->SysArticle->update($cond, [SysArticleConst::Status => 1]);
        // hook controller_zx_admin_content_articles_enable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }

    /**
     * @title   禁用
     * @rank    40
     */
    public function action_Disable_POST()
    {
        // hook controller_zx_admin_content_articles_disable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                isset($row['ArticleID']) && $cond['ArticleID'][]=intval($row['ArticleID']);
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->SysArticle->update($cond, [SysArticleConst::Status => 0]);
        // hook controller_zx_admin_content_articles_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    /**
     * @title   下拉选择属性
     * @rank    30
     */
    public function action_Option_GET()
    {
        // hook controller_zx_admin_content_articles_option_start.php

        $value = $this->request->param('value');
        $cond = [
            'OptionField'=>['LIKE'=>$value]
        ];
        $order = [];
        $list = $this->SysArticle->select($cond, $order, 1, 100);
        $data=[];
        foreach ($list as $row){
            $data[]=[
              'label'=>$row['OptionLabel'],
              'value'=>$row['OptionValue'],
            ];
        }
        // hook controller_zx_admin_content_articles_option_end.php
        $this->response('0000', ['data' => $data]);
    }
    
    
    

      
     /**
      * @title   删除数据
      * @rank    50
      */
    public function action_Index_Delete()
    {
        // hook controller_zx_admin_content_articles_delete_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
            isset($row['ArticleID']) && $cond['ArticleID'][]=intval($row['ArticleID']);
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->SysArticle->delete($cond);
        // hook controller_zx_admin_content_articles_delete_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }
    
    // hook controller_zx_admin_content_articles_end.php
}

?>